/**
 * Copyright (C) 2002-2006 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.kingcore.director.setting.encoding;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;

import wzw.manager.ClipboardManager;
import wzw.util.StringUtils;

/**
 * <p>字符串编码转换辅助工具。</p>
 * @author	WUZEWEN on 2006-4-29
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @see		Object#hashCode()
 * @see		HashMap
 * @since	JDK5
 */

public class StringEncoding extends JFrame {

	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JTextField stringField = null;
	private JLabel jLabel1 = null;
	private JScrollPane jScrollPane = null;
	private JTextArea unicodeField = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (stringField == null) {
			stringField = new JTextField();
			stringField.setBounds(new java.awt.Rectangle(96,23,368,25));
		}
		return stringField;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new java.awt.Rectangle(96,58,267,111));
			jScrollPane.setViewportView(getJTextArea());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextArea() {
		if (unicodeField == null) {
			unicodeField = new JTextArea();
		}
		return unicodeField;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new java.awt.Rectangle(377,58,101,29));
			jButton.setText("转换并复制");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						if( stringField.getText()!=null ){
							
							unicodeField.setText( StringUtils.toUnicode(stringField.getText() ));
							unicodeField.setSelectionStart(0);
							unicodeField.setSelectionEnd( unicodeField.getText().length() );
//							unicodeField.requestFocus();
							unicodeField.selectAll();
							ClipboardManager.setClipBoardContents( unicodeField.getText() );
						}
					} catch (UnsupportedEncodingException e1) {
						e1.printStackTrace();
					}
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new java.awt.Rectangle(370,95,113,29));
			jButton1.setText("转换属性文件");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					execPropertiesFileToUnicode();
				}
			});
		}
		return jButton1;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// Test
		StringEncoding se = new StringEncoding();
		se.setVisible( true );

	}

	/**
	 * This is the default constructor
	 */
	public StringEncoding() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(495, 231);
		this.setContentPane(getJContentPane());
		this.setTitle("字符标准编码器");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(new java.awt.Rectangle(19,58,72,23));
			jLabel1.setText("Unicode码：");
			jLabel = new JLabel();
			jLabel.setBounds(new java.awt.Rectangle(19,24,67,23));
			jLabel.setText("字符串：");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(getJTextField(), null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(getJButton(), null);
			jContentPane.add(getJButton1(), null);
		}
		return jContentPane;
	}

	/**
	 * 生成properties属性配置文件对应的Unicode编码文件。
	 *
	 */
	private void execPropertiesFileToUnicode(){
		// 文件选择
		String fileName="";
		String saveName="";
		JFileChooser jFileChooser = new JFileChooser(); 
		jFileChooser.setCurrentDirectory(new File("."));
		jFileChooser.setFileFilter(new FileFilter() {
			public boolean accept(File f) {
				return f.getName().toLowerCase().endsWith(".properties")
				|| f.isDirectory();
			}
			public String getDescription() {
				return "Properties Files";
			}
		});		

		// “打开”对话框
		//jFileChooser.showDialog(this,"打开...");
		jFileChooser.showOpenDialog(this);
		fileName=jFileChooser.getSelectedFile().getPath();
		if(fileName==null || fileName.trim().equals("")){
			return;
		}
		
		// 以“.properties”结尾或者中间含有“.properties.”，都不再加扩展名
		if( !fileName.endsWith(".properties") && fileName.indexOf(".properties.")<1 ){
			fileName=fileName+".properties";
		}

		// “另存为”对话框
		//jFileChooser.showSaveDialog(this);
		jFileChooser.showDialog(this,"另存为...");
		saveName=jFileChooser.getSelectedFile().getPath();
		if(saveName==null || saveName.trim().equals("") ){
			return;
		}
		// 必须以“.properties”为扩展名
		if( !saveName.endsWith(".properties")){
			saveName=saveName+".properties";
		}
		
		//转换文件
		//System.out.println("fileName="+fileName);
		if(fileName.equals(saveName)){
			JOptionPane.showMessageDialog(this,"原文件和生成的文件名称不能相同！","错误",JOptionPane.ERROR_MESSAGE);
			return;
		}
		File fileOri=new File( fileName );
		File fileRes=new File( saveName );
		if(fileRes.exists()){
			if(JOptionPane.showConfirmDialog(this,"该文件已经存在，覆盖已经存在的文件吗？","提示",JOptionPane.OK_CANCEL_OPTION)
					!=JOptionPane.OK_OPTION)
				return;
		}
		
		FileWriter fw ;
		BufferedWriter bw ;
		FileReader fr;
		BufferedReader br;

		
		try {
			
			fw=new FileWriter( fileRes );
			bw=new BufferedWriter( fw );
			
			bw.write("# this file is create by Unicode Tool, powered by KingCore on" + new Date());
			bw.newLine();//断行
			
			fr=new FileReader( fileOri );
			br=new BufferedReader(fr);
			
			String line=br.readLine();//读取一行数据
			while(line!=null){
				// System.out.println("line="+line);
				if( line!=null && !line.startsWith("#") && line.indexOf("=")>0 ){
					line = line.substring(0,line.indexOf("=")+1) + 
							StringUtils.toUnicode(line.substring(line.indexOf("=")+1)) ;
				}
				bw.write(line);
				bw.newLine();
				
				line=br.readLine();
			}
			
			fr.close();
			
			bw.flush();//将数据更新至文件
			fw.close();//关闭文件流
			

			JOptionPane.showMessageDialog(this,"文件转换成功，到原文件目录下查看！");

		} catch ( Exception e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(this,e.getMessage(),"操作失败",JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
		
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
